/**
 * 
 */
package cn.hbu.zlfw.exchange.controller;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.hbu.zlfw.annotation.OperatorLog;
import cn.hbu.zlfw.annotation.OperatorType;
import cn.hbu.zlfw.common.util.AjaxResponse;
import cn.hbu.zlfw.common.util.PageModel;
import cn.hbu.zlfw.common.util.SessionUtil;
import cn.hbu.zlfw.common.util.Utility;
import cn.hbu.zlfw.dic.service.DicSchoolService;
import cn.hbu.zlfw.dic.service.DicUserTypeService;
import cn.hbu.zlfw.exchange.entity.Chat;
import cn.hbu.zlfw.exchange.service.ChatService;
import cn.hbu.zlfw.patent.service.PatentService;

/**
 * @ClassName: ChatController
 * @Description: 在线交流controller
 * @author tiddar
 * @date 2018年8月14日
 *
 */
@RequestMapping("/chater")
@Controller
public class ChatController {
	/**
	 * 日志
	 */
	public final Logger logger = LoggerFactory.getLogger(PosterController.class);
	/**
	 * 注入HttpServletRequest
	 */
	@Autowired
	private HttpServletRequest request;
	@Autowired
	private ChatService chatService;
	@Autowired
	private DicSchoolService dicSchoolService;

	@Autowired
	private DicUserTypeService dicTypeService;
	@Autowired
	private PatentService patentService;

	@OperatorLog(type = OperatorType.SELECT, description = "进入在线交流界面")
	@RequestMapping({ "/toChatListView.do", "/toChatListView.action" })
	public String toChat(String forUserId, String patentName, String patentId, ModelMap map) {
		map.put("forUserId", forUserId);
		map.put("patentId", patentId);
		map.put("patentName", patentName);
		HttpSession session = request.getSession();
		session.setAttribute("patentId", patentId);
		return "post/chat";
	}

	@OperatorLog(type = OperatorType.SELECT, description = "进入在线交流历史界面")
	@RequestMapping({ "/toChatListHistoryView.do", "/toChatListHistoryView.action" })
	public String toChatHistory(String patentName,String patentId, ModelMap map) {
		map.put("patentId", patentId);
		map.put("patentName", patentName);
		HttpSession session = request.getSession();
		session.setAttribute("patentId", patentId);
		return "post/chatHistory";
	}

	@ResponseBody
	@OperatorLog(type = OperatorType.SAVE, description = "发送聊天")
	@RequestMapping({ "/submitChat.do", "/submitChat.action" })
	public String submitChat(String content, String patentName, String patentId, ModelMap map) {
		Chat chat = new Chat();
		chat.setContent(content);
		chat.setCreateTime(new Date());
		chat.setUpdateTime(new Date());
		chat.setDelState(0);
		chat.setUser(SessionUtil.getLoginUser(request));
		chatService.save(chat);
		return "success";
	}

	@ResponseBody
	@OperatorLog(type = OperatorType.SAVE, description = "分离获取聊天列表")
	@RequestMapping({ "/getChatList.do", "/getChatList.action" })
	public AjaxResponse getChatList(String patentId) {
		AjaxResponse result = AjaxResponse.instance();
		Map<String, Object> map = Utility.getMapInstance();
		PageModel<Chat> page = new PageModel<Chat>();
		page.setPageSize(9999);
		page = chatService.listChatToPatent(page, patentId);
		List<Chat> chats = page.getList();
		map.put("chatList", chats);
		result.setData(map);
		result.setSuccess(true);
		return result;
	}

	@ResponseBody
	@OperatorLog(type = OperatorType.SAVE, description = "分页获取聊天历史列表")
	@RequestMapping({ "/getChatListHis.do", "/getChatListHis.action" })
	public AjaxResponse getChatListHis(String patentId,
			@RequestParam(defaultValue="0",required=false)Integer pageNum) {
		AjaxResponse result = AjaxResponse.instance();
		Map<String, Object> map = Utility.getMapInstance();
		PageModel<Chat> page = new PageModel<Chat>();
		page.setPageSize(20);
		page.setCurrentPage(pageNum);
		page = chatService.listChatToPatent(page, patentId);
		List<Chat> chats = page.getList();
		map.put("chatList", chats);
		result.setData(map);
		result.setSuccess(true);
		return result;
	}
}
